package com.live.proxy.tool;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class OkHttpUtils {

    //发送jSON格式的请求
    public static void sendOkHttpJsonRequest(String url,String jsonStr ,okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), jsonStr);
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Content-Type", "application/json")
                .post(body)
                .build();
        client.newCall(request)
                .enqueue(callback);
    }

    //发送jSON格式的请求
    public static void sendOkHttpJsonTokenRequest(String url,String jsonStr ,okhttp3.Callback callback,String token) {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), jsonStr);
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Content-Type", "application/json")
                .addHeader("token", token)
                .post(body)
                .build();
        client.newCall(request)
                .enqueue(callback);
    }

    //发送字符串的请求
    public static void sendOkHttpStringRequest(String url,String str ,okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=utf-8"), str);
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Content-Type", "text/html;charset=utf-8")
                .post(body)
                .build();
        client.newCall(request)
                .enqueue(callback);
    }

}
